import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/gate_plan_view_entity.dart';

/// 企业图引列表显示
class FigureShadowWidget extends StatefulWidget {
  const FigureShadowWidget({required this.selectedFigureShadow, super.key});

  /// 选中的图影
  final List<GatePlanViewEntity> selectedFigureShadow;

  @override
  State<FigureShadowWidget> createState() => _FigureShadowWidgetState();
}

class _FigureShadowWidgetState extends State<FigureShadowWidget> {
  @override
  Widget build(BuildContext context) {
    return CustomEasyRefreshList<GatePlanViewEntity>(
      enableLoad: false,
      dataLoad: (pageIndex, pageSize, {String? search}) async {
        return _getListData(pageIndex, pageSize, search: search);
      },
      itemBuilder: (GatePlanViewEntity item, int index) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(8.w)),
            color: const Color(0xffEEF0FF),
          ),
          child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Row(
              children: [
                Expanded(
                    child: Text(
                  item.viewName,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xff5C6DFF),
                    fontWeight: FontWeight.w500,
                  ),
                )),
                SvgPicture.asset(
                  widget.selectedFigureShadow.any((obj) => obj.id == item.id) ? AppImages.icon_selection_box : AppImages.icon_un_selection_box,
                  width: 16.w,
                  height: 16.w,
                ),
              ],
            ),
            Container(
              margin: EdgeInsets.only(top: 8.w, right: 8.w),
              width: 140.w,
              height: 80.w,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4.w),
                  color: Colors.white,
                  border: Border.all(
                    color: const Color(0xFFEFF2F6),
                    width: 1,
                  ),
                  image: DecorationImage(
                    image: NetworkImage(Interface.fileUrl + item.planView),
                  )),
            ).clickShowLarge(url: Interface.fileUrl + item.planView),
          ]),
        ).clickOnce(() {
          if (widget.selectedFigureShadow.any((element) => element.id == item.id)) {
            widget.selectedFigureShadow.removeWhere((element) => element.id == item.id);
          } else {
            widget.selectedFigureShadow.add(item);
          }
          setState(() {});
        });
      },
    );
  }

  /// 获取列表数据
  Future<List<GatePlanViewEntity>> _getListData(int pageNum, int pageSize, {String? search}) async {
    // 如果是刷新
    if (pageNum == 1) {
      widget.selectedFigureShadow.clear();
    }
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<GatePlanViewEntity>>(
      Interface.getPlanViewList,
      queryParameters: {
        'pageNum': 1,
        'pageSize': 200,
      },
    ).execute();
    List<GatePlanViewEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }
}
